<template>
  <span>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" :append-to-body="true">
      <el-form ref="scriptForm" :model="scriptForm" :rules="rulesScriptForm">
        <el-form-item label="脚本名称" :label-width="formLabelWidth" prop="scriptName">
          <el-input v-model="scriptForm.scriptName" autocomplete="off" placeholder="请输入名称"></el-input>
        </el-form-item>
        <el-form-item label="代码仓库" :label-width="formLabelWidth" prop="codeRepository">
          <el-input v-model="scriptForm.codeRepository" autocomplete="off" placeholder="请输入git地址"></el-input>
        </el-form-item>
        <el-form-item label="脚本描述" :label-width="formLabelWidth" prop="description">
          <el-input v-model="scriptForm.description" autocomplete="off" placeholder="描述脚本用途" type="textarea"  autosize></el-input>
        </el-form-item>
        <el-form-item label="所属项目/产品" :label-width="formLabelWidth" prop="product">
          <el-input v-model="scriptForm.product" autocomplete="off" placeholder="所属产品" type="textarea"  autosize></el-input>
        </el-form-item>
        <el-form-item label="功能模块" :label-width="formLabelWidth" prop="module">
          <el-input v-model="scriptForm.module" autocomplete="off" placeholder="产品功能模块" type="textarea"  autosize></el-input>
        </el-form-item>
        <el-form-item label="jenkins名称" :label-width="formLabelWidth" prop="jenkinsName">
          <el-select v-model="scriptForm.jenkinsName" clearable placeholder="请选择jenkins服务器名" @change="selectJenkins">
            <el-option
              v-for="item in jenkinsConfig"
              :key="item.id"
              :label="item.jenkinsName"
              :value="item.jenkinsName">
              <span style="float: left">{{ item.jenkinsName }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.jenkinsUrl }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="job全名" :label-width="formLabelWidth" prop="jobFullName">
          <el-input v-model="scriptForm.jobFullName" autocomplete="off" placeholder="Full project name" type="text" autosize></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small"  @click="dialogFormVisible = false"> 取 消 </el-button>
        <el-button size="small"  type="primary" @click="handleOK"> 确 定 </el-button>
      </div>
    </el-dialog>
  </span>
</template>

<script>

import {addScript, editScript} from "@/api/script";
import {getJenkinsList} from "@/api/jenkins";

export default {
  name: "ScriptTask",
  props: {
    title: {
      type: String,
      default: ''
    },
    operate: {
      type: String,
      default: 'edit'
    }
  },
  data() {
    return {
      dialogFormVisible: false,
      jenkinsConfig: null,
      scriptForm: {
        id: '',
        scriptName: '',
        codeRepository: '',
        description: '',
        product: '',
        module: '',
        jenkinsName: '',
        jobFullName: ''
      },
      rulesScriptForm: {
        scriptName: [
          { required: true, message: '请填写脚本名称', trigger: 'blur' },
        ],
        codeRepository: [
          { required: true, message: '请输入仓库git地址', trigger: 'blur' }
        ]
      },
      formLabelWidth: '120px'
    }
  },
  methods: {
    fetchJenkinsConfig() {
      getJenkinsList().then(res => {
        this.jenkinsConfig = res.data
      })
    },
    selectJenkins: function (value) {
      this.scriptForm.jenkinsName = value
      console.log(value)
    },
    showDialog: function (index=0, row={}) {
      this.dialogFormVisible = true
      this.fetchJenkinsConfig()
      if (this.operate === 'edit') {
        this.scriptForm.id = row.scriptId
        this.scriptForm.scriptName = row.scriptName
        this.scriptForm.codeRepository = row.codeRepository
        this.scriptForm.description = row.description
        this.scriptForm.product = row.product
        this.scriptForm.module = row.module
        this.scriptForm.jenkinsName = row.jenkinsName
        this.scriptForm.jobFullName = row.jobFullName
      }
    },
    handleOK() {
      if (this.operate === 'add') {
        this.addData()
      } else if (this.operate === 'edit') {
        this.editData()
      }
      this.dialogFormVisible = false
    },
    addData() {
      this.$refs.scriptForm.validate((valid) => {
        if (valid) {
          let params = {
            scriptName: this.scriptForm.scriptName,
            codeRepository: this.scriptForm.codeRepository,
            product: this.scriptForm.product,
            module: this.scriptForm.module,
            description: this.scriptForm.description,
            jenkinsName: this.scriptForm.jenkinsName,
            jobFullName: this.scriptForm.jobFullName
          }
          console.log(params)
          addScript(params).then(res => {
            if (res.code === 200) {
              this.$message({
                type: 'success',
                message: '添加成功'
              });
              this.$emit('fetchScriptData')
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              });
            }
          })
        }
      });
    },
    editData() {
      let params = {
        id: this.scriptForm.id,
        scriptName: this.scriptForm.scriptName,
        codeRepository: this.scriptForm.codeRepository,
        product: this.scriptForm.product,
        module: this.scriptForm.module,
        description: this.scriptForm.description,
        jenkinsName: this.scriptForm.jenkinsName,
        jobFullName: this.scriptForm.jobFullName
      }
      console.log(params)
      editScript(params).then(res => {
        console.log(res)
        if (res.code === 200) {
          this.$message({
            type: 'success',
            message: '修改成功'
          });
          this.$emit('fetchScriptData')
        } else {
          this.$message({
            type: 'error',
            message: res.msg
          });
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
